﻿function AzureOnlineBlobMetadataView(context) {

    var self = this;

    var dispatcher = $({});

    var spinner = new SpinnerModel();

    var container = ko.observable('');
    
    var blob = ko.observable(null);

    var editor = new AzureOnlineMetaEditor(context);

    function init(blobModel) {
        blob(blobModel);
        container(context.blobs.container());
        editor.init(blobModel.metadata());
    }

    function submit() {
        var api = context.api();
        if (editor.validate()) {
            var data = editor.getInfo();
            spinner.enter();
            api.blobs.setBlobMetadata({
                metadata: data,
                container: container(),
                blob: blob().name(),
                success: function() {
                    spinner.leave();
                    blob().metadata(data);
                    dispatcher.trigger('success');
                },
                error: function() {
                    spinner.leave();
                }
            });
        }
    }

    self.blob = blob;
    self.rows = editor.rows;
    self.init = init;
    self.submit = submit;

    self.addOnSuccess = function (handler) {
        dispatcher.bind('success', handler);
    };
}